package com.cskaoyan.config.realm;

import com.cskaoyan.bean.PO.*;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketPermissionMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CustomRealm
 * @Description
 * @Author Lxz
 * @Date 2022-8-16 8:53
 */
@Component
public class CustomRealm extends AuthorizingRealm {
    @Autowired
    MarketAdminMapper adminMapper;
    @Autowired
    MarketUserMapper userMapper;
    @Autowired
    MarketPermissionMapper permissionMapper;



    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String type = ((MarketToken) authenticationToken).getType();
        String username = (String) authenticationToken.getPrincipal();
        if ("admin".equals(type)) {
            MarketAdminExample example = new MarketAdminExample();
            example.createCriteria().andUsernameEqualTo(username);
            List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);
            if (marketAdmins.size() == 1) {
                MarketAdmin marketAdmin = marketAdmins.get(0);
                return new SimpleAuthenticationInfo(marketAdmin,marketAdmin.getPassword(),getName());
            }
        } else if ("wx".equals(type)) {
            MarketUserExample example = new MarketUserExample();
            example.createCriteria().andUsernameEqualTo(username);
            List<MarketUser> marketUsers = userMapper.selectByExample(example);
            if (marketUsers.size() == 1) {
                MarketUser marketUser = marketUsers.get(0);
                return new SimpleAuthenticationInfo(marketUser,marketUser.getPassword(),getName());

            }
        }

        return null;

    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        MarketAdmin principal = (MarketAdmin) principalCollection.getPrimaryPrincipal();
        List<String> permissions = getPermissionsByUsername(principal);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(permissions);
        return simpleAuthorizationInfo;
    }

    private List<String> getPermissionsByUsername(MarketAdmin principal) {
        MarketAdminExample example = new MarketAdminExample();
        example.createCriteria().andUsernameEqualTo(principal.getUsername());
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);
        Integer[] roleIds = marketAdmins.get(0).getRoleIds();
        ArrayList<String> permissions = new ArrayList<>();
        MarketPermissionExample permissionExample= new MarketPermissionExample();
        for (int i = 0; i < roleIds.length; i++) {
            permissionExample.createCriteria().andRoleIdEqualTo(roleIds[i]);
            List<MarketPermission> marketPermissionList = permissionMapper.selectByExample(permissionExample);
            for (MarketPermission marketPermission : marketPermissionList) {
                permissions.add(marketPermission.getPermission());
            }
        }
        return permissions;
    }


}
